﻿using AbpApplication.Expand;
using AbpApplication.InPut;
using AbpToDomain.EntityInfo;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace AbpApplication.AppServices
{
    public class CusetomService : ApplicationService, ICustomService
    {

        private readonly IRepository<TaskOrder> taskorder_res;
        private readonly IRepository<NeetOutMaterial> neetout_res;
        private readonly IRepository<MaterialBase> material_res;
        private readonly IRepository<NeetOutMaterial> neetoutmaterial_res;
        private readonly IRepository<CusetomOrder> cusetom_order_res;
        private readonly IRepository<CusetomOrderLine> cusetom_order_line_res;
        private readonly ILoginHelper loginHelper;

        public CusetomService(IRepository<TaskOrder> taskorder_res,
            IRepository<NeetOutMaterial> neetout_res,
            IRepository<MaterialBase> material_res,
            IRepository<NeetOutMaterial> neetoutmaterial_res, ILoginHelper loginHelper, 
            IRepository<CusetomOrder> cusetom_order_res, 
            IRepository<CusetomOrderLine> cusetom_order_line_res)

        {
            this.taskorder_res = taskorder_res;
            this.neetout_res = neetout_res;
            this.material_res = material_res;
            this.neetoutmaterial_res = neetoutmaterial_res;
            this.loginHelper = loginHelper;
            this.cusetom_order_res = cusetom_order_res;
            this.cusetom_order_line_res = cusetom_order_line_res;
        }

        public async Task<ApiResponseCustom> CreateTaskOrderAuto(InPut_CustomService InPut)//自动出库
        {
            try
            {
                string result = String.Join(", ", InPut.outItems.Select(i => $"ReelID: {i.reelId}, SlotNum: {i.slotNum}, Quantity: {i.quantity}, MtrlCode: {i.mtrlCode}"));
                Logger.LogWarning("工单记录-"+"mocode:"+InPut.moCode+"-"+"so:"+InPut.so+"-"+ "outitems:" + result);
                if (string.IsNullOrEmpty(InPut.moCode)) return new ApiResponseCustom("1", "没有LPN明细", null);
                if (string.IsNullOrEmpty(InPut.hSerial)||string.IsNullOrEmpty(InPut.so)) return new ApiResponseCustom("1", "请填写需求单号", null);
                bool boolres = await cusetom_order_res.AnyAsync(x => x.hSerial== InPut.hSerial);
                if (boolres) return new ApiResponseCustom("1", "工单号已存在", null);
                CusetomOrder cusetomOrder =new CusetomOrder();
                cusetomOrder= cusetomOrder.AddCusetomOrder(InPut.hSerial, InPut.moCode,InPut.so);
                List<CusetomOrderLine> cusetomOrderLines = new List<CusetomOrderLine>();
                foreach (var item in InPut.outItems)
                {
                    CusetomOrderLine cusetomOrderLine = new CusetomOrderLine();
                    cusetomOrderLine = cusetomOrderLine.AddCusetomOrderLine(item.reelId, item.quantity.ToString(), item.slotNum,item.mtrlCode);
                    cusetomOrderLines.Add(cusetomOrderLine);
                }
                cusetomOrder.cusetomOrderLines= cusetomOrderLines;
                await cusetom_order_line_res.InsertManyAsync(cusetomOrderLines);
                await cusetom_order_res.InsertAsync(cusetomOrder);
                return new ApiResponseCustom("0", "创建成功", null);
            }
            catch(Exception ex) 
            {
                loginHelper.LoginAsync(InPut, "CreateTaskOrderAuto");
                return new ApiResponseCustom("500", "服务器内部错误", null);
            }
   
        }
        public async Task<ApiResponseCustom> RemoveTaskOrderAuto(string hSerial)
        {
            try
            {
                TaskOrder taskOrder = await taskorder_res.FirstOrDefaultAsync(x => x.OrderNo == hSerial && x.State == "未下发");
                if (taskOrder == null) return new ApiResponseCustom("0", "未查询到可以取消的任务", null);
                await taskorder_res.DeleteAsync(taskOrder);
                List<NeetOutMaterial> neetOutMaterials = await neetoutmaterial_res.GetListAsync(x => x.OrderNo == hSerial);
                await neetoutmaterial_res.DeleteManyAsync(neetOutMaterials);
                return new ApiResponseCustom("0", "取消成功", null);
            }
            catch(Exception ex)
            {
                Logger.LogWarning($"RemoveTaskOrderAuto-{hSerial}" + ex.Message);
                return new ApiResponseCustom("400", "服务器内部错误", null);
            }
 
        }//删除出库订单

    }
}
